﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using Common.Constants;
using Common.Helper;
using Entities;

namespace DataAccess
{
    public class AddressDataAccess
    {
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static AddressDataAccess Instance { get; private set; }

        /// <summary>
        /// Initializes the <see cref="AddressDataAccess" /> class.
        /// </summary>
        static AddressDataAccess() { Instance = new AddressDataAccess(); }



        /// <summary>
        /// Extracts the raw data.
        /// </summary>
        /// <param name="dataReader">The data reader.</param>
        /// <returns></returns>
        private static Address ExtractRawData(IDataRecord dataReader)
        {
            var address = new Address();

            address.AddressId = (Guid)dataReader["AddressId"];
            address.Street = dataReader["Street"].ToString();
            address.PostCode = dataReader["PostCode"].ToString();
            if (dataReader["CountyId"] != null)
                address.CountyId = (Guid)dataReader["CountyId"];
            if (dataReader["CountryId"] != null)
                address.CountryId = (Guid)dataReader["CountryId"];
            if (dataReader["TownID"] != null)
                address.TownId = (Guid)dataReader["TownID"];

            return address;


        }
        public Address GetAddress(string addreesId)
        {


            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("AddressId", addreesId)
                                      
                                       
                                   };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetAddress, colParameter))
            {
                if (reader.Read())
                {
                    return ExtractRawData(reader);

                }
                return null;
            }
        }
        /// <summary>
        /// Lists the Address
        /// </summary>
        /// <param name="street">The street.</param>
        /// <param name="postCode">The post code.</param>
        /// <param name="town">The town.</param>
        /// <returns></returns>
        public Collection<Address> ListAddress(string street, string postCode, string town)
        {
            var lstAddress = new Collection<Address>();

            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("Street", street),
                                       new SqlParameter("PostCode",postCode),
                                       new SqlParameter("Town",town)
                                       
                                   };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.ListAddress, colParameter))
            {
                while (reader.Read())
                {
                    var result = ExtractRawData(reader);
                    lstAddress.Add(result);
                }
                return lstAddress;
            }
        }

        public Address GetAddressByPostcode(string postCode)
        {
            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("PostCode", postCode),
                                   };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetAddressByPostcode, colParameter))
            {
                if (reader.Read())
                {
                    return ExtractRawData(reader);
                }
                return null;
            }

        }
    }
}
